﻿using FarmSmartSleep.Common.Domain.BeginTable;
using FarmSmartSleep.Common.Domain;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;

namespace FarmSmartSleep.Organ.API.Application.SleepInfoCommands.RecordCommands
{
    public class GetSleepInfoHandler : IRequestHandler<GetSleepInfoCommand, ResultT<SleepRecordDto>>
    {
        //栋舍
        private readonly IBaseRepository<Cottages> cottagebaseRepository;
        //栏位
        private readonly IBaseRepository<Fields> fieldsRepository;
        //羊场
        private readonly IBaseRepository<SheepFarm> farmbaseRepository;
        //品种
        private readonly IBaseRepository<Variety> VarietybaseRepository;
        //基因等级
        private readonly IBaseRepository<GeneGrade> geneGradeRepository;
        //羊只类型
        private readonly IBaseRepository<SleepType> sleeptypebaseRepository;

        //档案信息
        private readonly IBaseRepository<SleepInfor> sleepinforbaseRepository;
        //出生状态
        private readonly IBaseRepository<BirthStatus> birthstatubaseRepository;
        //栋舍类型
        private readonly IBaseRepository<CottageType> cottageTypeRepository;



        public GetSleepInfoHandler(IBaseRepository<Cottages> cottagebaseRepository, IBaseRepository<Variety> varietybaseRepository,
                                        IBaseRepository<GeneGrade> geneGradeRepository,
                                        IBaseRepository<Fields> fieldsRepository, IBaseRepository<FemaleBegin> femaleBeginRepository,
                                        IBaseRepository<SleepInfor> sleepinforbaseRepository,
                                        IBaseRepository<BirthStatus> birthstatubaseRepository, IBaseRepository<SheepFarm> farmbaseRepository,
                                        IBaseRepository<SleepType> sleeptypebaseRepository, IBaseRepository<CottageType> cottageTypeRepository)

        {
            this.cottagebaseRepository = cottagebaseRepository;
            this.VarietybaseRepository = varietybaseRepository;
            this.geneGradeRepository = geneGradeRepository;
            this.fieldsRepository = fieldsRepository;
            this.sleepinforbaseRepository = sleepinforbaseRepository;
            this.birthstatubaseRepository = birthstatubaseRepository;
            this.farmbaseRepository = farmbaseRepository;
            this.sleeptypebaseRepository = sleeptypebaseRepository;
            this.cottageTypeRepository = cottageTypeRepository;

        }
        public async Task<ResultT<SleepRecordDto>> Handle(GetSleepInfoCommand request, CancellationToken cancellationToken)
        {
            var result = new ResultT<SleepRecordDto>() { Code = (int)CodeEnum.search };
            SleepRecordDto res = new SleepRecordDto();
            var list = (from a in sleepinforbaseRepository.GetAll()
                        join b in farmbaseRepository.GetAll() on a.FarmId equals b.FarmId
                        join c in cottagebaseRepository.GetAll() on a.RecordcottageId equals c.CottagesId
                        join d in fieldsRepository.GetAll() on a.FieIdId equals d.FieIdId
                        join e in sleeptypebaseRepository.GetAll() on a.SleepTypeId equals e.SleepTypeId
                        join f in birthstatubaseRepository.GetAll() on a.BirthdayId equals f.BirthdayId
                        join g in VarietybaseRepository.GetAll() on a.VarietyId equals g.VarietyId
                        join h in geneGradeRepository.GetAll() on a.GradeId equals h.GradeId
                        join i in cottageTypeRepository.GetAll() on c.CottageTypeId equals i.CottageTypeId
                        where a.SleepInforId==request.sleepInforId
                        select new SleepRecordDto()
                        {
                            EarNumber = a.EarNumber,
                            VarietyId = a.VarietyId,
                            ElectronicEarTag = a.ElectronicEarTag,
                            GradeId = a.GradeId,
                            SheepImage = a.SheepImage,
                            Gender = a.Gender,
                            SleepTypeId = a.SleepTypeId,
                            StayStatus = a.StayStatus,
                            FarmId = a.FarmId,
                            RecordcottageId = a.RecordcottageId,
                            FieIdId = a.FieIdId,
                            FarEarNumber = a.FarEarNumber,
                            MoEarNumber = a.MoEarNumber,
                            SleepBirthDate = a.SleepBirthDate,
                            SleepEntryDate = a.SleepEntryDate,
                            SleepBirthPlace = a.SleepBirthPlace,
                            SleepBirthWeight = a.SleepBirthWeight,
                            SleepNotes = a.SleepNotes,
                            BirthdayId = a.BirthdayId,
                            SleepTypeName = e.SleepTypeName,
                            BirsdayName = f.BirsdayName,
                            Grade = h.Grade,
                            CottageTypeId = i.CottageTypeId,
                            CottageName = c.CottageName,
                            FieIdName = d.FieIdName,
                            BreedStatuId = a.BreedStatuId,
                            BreedDayNum = a.BreedDayNum,
                            CheckPre = a.CheckPre,
                            ProductLevel = a.ProductLevel,
                            BuyLevel = a.BuyLevel,
                            WeanWeight = a.WeanWeight,
                            WeanData = a.WeanData,
                            agetotal = (int)(DateTime.Now - Convert.ToDateTime(a.SleepBirthDate)).TotalDays,
                        }).FirstOrDefault();
            if(list == null)
            {
                res = (from a in sleepinforbaseRepository.GetAll()
                       join b in farmbaseRepository.GetAll() on a.FarmId equals b.FarmId
                       join e in sleeptypebaseRepository.GetAll() on a.SleepTypeId equals e.SleepTypeId
                       join g in VarietybaseRepository.GetAll() on a.VarietyId equals g.VarietyId
                       join h in geneGradeRepository.GetAll() on a.GradeId equals h.GradeId
                       where a.SleepInforId == request.sleepInforId
                       select new SleepRecordDto()
                       {
                           SleepInforId = a.SleepInforId,
                           EarNumber = a.EarNumber,
                           VarietyId = a.VarietyId,
                           ElectronicEarTag = a.ElectronicEarTag,
                           GradeId = a.GradeId,
                           SheepImage = a.SheepImage,
                           Gender = a.Gender,
                           SleepTypeId = a.SleepTypeId,
                           StayStatus = a.StayStatus,
                           FarmId = a.FarmId,
                           RecordcottageId = a.RecordcottageId,
                           FieIdId = a.FieIdId,
                           FarEarNumber = a.FarEarNumber,
                           MoEarNumber = a.MoEarNumber,
                           SleepBirthDate = a.SleepBirthDate,
                           SleepEntryDate = a.SleepEntryDate,
                           SleepBirthPlace = a.SleepBirthPlace,
                           SleepBirthWeight = a.SleepBirthWeight,
                           SleepNotes = a.SleepNotes,
                           BirthdayId = a.BirthdayId,
                           SleepTypeName = e.SleepTypeName,
                           Grade = h.Grade,
                           BreedStatuId = a.BreedStatuId,
                           BreedDayNum = a.BreedDayNum,
                           CheckPre = a.CheckPre,
                           ProductLevel = a.ProductLevel,
                           BuyLevel = a.BuyLevel,
                           WeanWeight = a.WeanWeight,
                           WeanData = a.WeanData,
                           agetotal = (int)(DateTime.Now - Convert.ToDateTime(a.SleepBirthDate)).TotalDays,

                           VarietyName = g.VarietyName
                       }).FirstOrDefault();
            }
            else
            {
                 res = (from a in sleepinforbaseRepository.GetAll()
                            join b in farmbaseRepository.GetAll() on a.FarmId equals b.FarmId
                            join c in cottagebaseRepository.GetAll() on a.RecordcottageId equals c.CottagesId
                            join d in fieldsRepository.GetAll() on a.FieIdId equals d.FieIdId
                            join e in sleeptypebaseRepository.GetAll() on a.SleepTypeId equals e.SleepTypeId
                            join f in birthstatubaseRepository.GetAll() on a.BirthdayId equals f.BirthdayId
                            join g in VarietybaseRepository.GetAll() on a.VarietyId equals g.VarietyId
                            join h in geneGradeRepository.GetAll() on a.GradeId equals h.GradeId
                            join i in cottageTypeRepository.GetAll() on c.CottageTypeId equals i.CottageTypeId
                            where a.SleepInforId == request.sleepInforId
                        select new SleepRecordDto()
                            {
                                EarNumber = a.EarNumber,
                                VarietyId = a.VarietyId,
                                ElectronicEarTag = a.ElectronicEarTag,
                                GradeId = a.GradeId,
                                SheepImage = a.SheepImage,
                                Gender = a.Gender,
                                SleepTypeId = a.SleepTypeId,
                                StayStatus = a.StayStatus,
                                FarmId = a.FarmId,
                                RecordcottageId = a.RecordcottageId,
                                FieIdId = a.FieIdId,
                                FarEarNumber = a.FarEarNumber,
                                MoEarNumber = a.MoEarNumber,
                                SleepBirthDate = a.SleepBirthDate,
                                SleepEntryDate = a.SleepEntryDate,
                                SleepBirthPlace = a.SleepBirthPlace,
                                SleepBirthWeight = a.SleepBirthWeight,
                                SleepNotes = a.SleepNotes,
                                BirthdayId = a.BirthdayId,
                                SleepTypeName = e.SleepTypeName,
                                BirsdayName = f.BirsdayName,
                                Grade = h.Grade,
                                CottageTypeId = i.CottageTypeId,
                                CottageName = c.CottageName,
                                FieIdName = d.FieIdName,
                                BreedStatuId = a.BreedStatuId,
                                BreedDayNum = a.BreedDayNum,
                                CheckPre = a.CheckPre,
                                ProductLevel = a.ProductLevel,
                                BuyLevel = a.BuyLevel,
                                WeanWeight = a.WeanWeight,
                                WeanData = a.WeanData,
                                agetotal = (int)(DateTime.Now - Convert.ToDateTime(a.SleepBirthDate)).TotalDays,
                        }).FirstOrDefault();
            }
            result.Item = res;
            return result;
        }
    }
}
